
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
		<script type="text/javascript" src="jquery.min.js"></script>
		<style type="text/css">
			.title {
				width: 200px;
				margin-top: 10px;
			}
			
			.text {
				width: 300px;
				margin-top: 10px;
			}
			
			.button {
				width: 300px;
				margin-top: 10px;
			}
		</style>
		<script type="text/javascript">
			function postJson(json) {
				var address = "http://" + $("#SERVER_ADDR").val() + ":" + $("#SERVER_PORT").val();
				var params = JSON.stringify(json);
				$("#SEND_TXT").text(params);
				var yz = $.ajax({
					type: 'post',
					url: address,
					data: json,
					cache: false,
					dataType: 'json',
					complete: function(data, status) {
						$("#RECEIVE_TXT").text(data.responseText);
					},
					error: function(request, status, thrown) {
						$("#RECEIVE_TXT").text(status);
					}
				});
			};

			function dateConvert(dateParms) {
				if(dateParms instanceof Date) {
					var datetime = dateParms;
				}
				if((typeof dateParms == "string") && dateParms.constructor == String) {
					var datetime = new Date(Date.parse(dateParms.replace(/-/g, "/")));
				}

				var year = datetime.getFullYear().toString();
				var month = (datetime.getMonth() + 1).toString();
				var day = datetime.getDate().toString();
				var hour = datetime.getHours().toString();
				var minutes = datetime.getMinutes().toString();
				var second = datetime.getSeconds().toString();

				if(month < 10) {
					month = "0" + month;
				}
				if(day < 10) {
					day = "0" + day;
				}
				if(hour < 10) {
					hour = "0" + hour;
				}
				if(minutes < 10) {
					minutes = "0" + minutes;
				}
				if(second < 10) {
					second = "0" + second;
				}

				var time = year + month + day;
				return time;
			}
			//检查设备在线
			function checkDevice() {
				var json = {};
				json.type = "accessV2";
				json.active = "checkdevice";
				json.device = $('#DEVICE_SN').val();
				json.password = $('#DEVICE_PWD').val();
				postJson(json);
			}
			//远程开门
			function remoteDoor(door) {
				var json = {};
				json.type = "accessV2";
				json.active = "remotedoor";
				json.device = $('#DEVICE_SN').val();
				json.password = $('#DEVICE_PWD').val();
				json.door = door;
				postJson(json);
			};
			//开门
			function openDoor(door) {
				var json = {};
				json.type = "accessV2";
				json.active = "opendoor";
				json.device = $('#DEVICE_SN').val();
				json.password = $('#DEVICE_PWD').val();
				json.door = door;
				postJson(json);
			};
			//关门
			function closeDoor(door) {
				var json = {};
				json.type = "accessV2";
				json.active = "closedoor";
				json.device = $('#DEVICE_SN').val();
				json.password = $('#DEVICE_PWD').val();
				json.door = door;
				postJson(json);
			};
			//恢复在线
			function resetDoor(door) {
				var json = {};
				json.type = "accessV2";
				json.active = "resetdoor";
				json.device = $('#DEVICE_SN').val();
				json.password = $('#DEVICE_PWD').val();
				json.door = door;
				postJson(json);
			};
			//下发权限
			function addRegister(door, name, id, card, password, dateBegin, dateEnd) {
				var json = {};
				json.type = "accessV2";
				json.active = "addregister";
				json.device = $('#DEVICE_SN').val();
				json.password = $('#DEVICE_PWD').val();
				json.door = door;
				json.username = name;
				json.userid = id;
				json.cardno = card;
				json.userpassword = password;
				json.datebegin = dateConvert(dateBegin);
				json.dateend = dateConvert(dateEnd);
				json.timegroup = 1;
				postJson(json);
			};
			//删除权限
			function removeRegister(door, card) {
				var json = {};
				json.type = "accessV2";
				json.active = "removeregister";
				json.device = $('#DEVICE_SN').val();
				json.password = $('#DEVICE_PWD').val();
				json.door = door;
				json.cardno = card;
				alert(card);
				postJson(json);
			};
			//读取记录
			function readLog(index) {
				var json = {};
				json.type = "accessV2";
				json.active = "readlog";
				json.device = $('#DEVICE_SN').val();
				json.password = $('#DEVICE_PWD').val();
				json.index = index;
				postJson(json);
			};
		</script>
	</head>

	<body>
		<div style="margin: 0 auto; text-align: center; width: 100%; height: 30px;" id="myDiv">
			<h2>门禁盒子WebAPI接口测试</h2>
			<label class="title">WebAPI接入地址:</label>
			<input class="text" type="text" id="SERVER_ADDR" name="SERVER_ADDR" value="192.168.31.222" /><br/>
			<label class="title">WebAPI接入端口:</label>
			<input class="text" type="number" id="SERVER_PORT" name="SERVER_PORT" value="40003" /><br/>
			<label>发送内容</label><br />
			<textarea id="SEND_TXT" disabled="disabled" name="SEND_TXT" style="width: 80%; height: 80px; color: red;"></textarea>
			<br />
			<label>接收内容</label><br />
			<textarea id="RECEIVE_TXT" disabled="disabled" name="RECEIVE_TXT" style="width: 80%; height: 80px; color: red;"></textarea><br />
			<label>(Sn:设备序列号 Success:true成功,false失败 Msg:详情)</label>
			<hr>
			<label class="title">设备SN号:</label>
			<input class="text" type="number" id="DEVICE_SN" name="DEVICE_SN" value="2209009" /><br/>
			<label class="title">设备密码:</label>
			<input class="text" type="text" id="DEVICE_PWD" name="DEVICE_PWD" value="FFFFFF" /><br/>
			<input class="button" type="button" onclick='checkDevice();' value="查询设备是否在线" />
			<h3>设置紧急开关门</h3>
			<label class="title">门编号:</label>
			<input class="text" type="number" id="DOOR_OPERATE" name="DOOR_OPERATE" value="2" /><br/>
			<input class="button" type="button" onclick="remoteDoor($('#DOOR_OPERATE').val());" value="远程开门" />
			<input class="button" type="button" onclick="openDoor($('#DOOR_OPERATE').val());" value="紧急开门" />
			<input class="button" type="button" onclick="closeDoor($('#DOOR_OPERATE').val());" value="紧急关门" />
			<input class="button" type="button" onclick="resetDoor($('#DOOR_OPERATE').val());" value="恢复正常" />
			<h3>权限管理</h3>
			<label class="title">门号:</label>
			<input class="text" type="number" id="DOOR_REGISTER" name="DOOR_REGISTER" value="2" /><br/>
			<label class="title">姓名:</label>
			<input class="text" type="text" id="USER_NAME" name="USER_NAME" value="Seven" /><br/>
			<label class="title">工号:</label>
			<input class="text" type="text" id="USER_ID" name="USER_ID" value="N001" /><br/>
			<label class="title">卡号:</label>
			<input class="text" type="number" id="USER_CARD" name="USER_CARD" value="123456" /><br/>
			<label class="title">密码:</label>
			<input class="text" type="text" id="USER_PASSWORD" name="USER_PASSWORD" value="FFFFFF" /><br/>
			<label class="title">有效期开始:</label>
			<input class="text" type="date" id="USEFUL_BEGIN" name="USEFUL_BEGIN" value="2017-09-01" /><br/>
			<label class="title">有效期结束:</label>
			<input class="text" type="date" id="USEFUL_END" name="USEFUL_END" value="2017-10-01" /><br/>
			<input class="button" type="button" onclick="addRegister($('#DOOR_REGISTER').val(),$('#USER_NAME').val(),$('#USER_ID').val(),$('#USER_CARD').val(),$('USER_PASSWORD').val(),$('#USEFUL_BEGIN').val(),$('#USEFUL_END').val());" value="新增权限" />
			<input class="button" type="button" onclick="removeRegister($('#DOOR_REGISTER').val(),$('#USER_CARD').val());" value="删除权限" />
			<h3>记录管理</h3>
			<label class="title">记录索引:</label>
			<input class="text" type="number" id="LOG_INDEX" name="LOG_INDEX" value="2" /><br/>
			<input class="button" type="button" onclick='readLog($(LOG_INDEX).val());' value="读取记录" />
		</div>
	</body>

</html>